package com.situ.day12;

import org.junit.Test;

import java.lang.String;
import java.sql.*;
import java.util.ArrayList;

public class JDBCDemo {

    @Test
    public void test1() {
        try {
            // 1、加载驱动Class.forName("");
            Class.forName("com.mysql.jdbc.Driver");
            // 2、获得连接对象Connection
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
            // 3、写sql语句
            String sql = "SELECT id,`name`,age,gender FROM student";
            // 4、创建Statement(一艘船)
            Statement statement = connection.createStatement();
            // 5、执行sql语句
            //         (1) 更新类（更改了表里面数据）：delete/update/insert         executeUpdate()
            // 返回值：int，表示你影响的行数
            //         (2)查询（没有改变表里面数据）:  select                       executeQuery()
            // 返回值：结果集ResultSet
            ResultSet resultSet = statement.executeQuery(sql);
            ArrayList<Student> list = new ArrayList<>();
            // next()方法判断下一行有没有，如果有指向下一行
            while (resultSet.next()) {
                // 每遍历一次循环，就拿出数据库查询结果里面的一行记录，就是一个学生的信息,
                // 就可以封装成一个Student对象
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
            // 6、关闭连接
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        // SELECT id,`name`,age,gender FROM student
        // ArrayList<Student> list
    }

    @Test
    public void test2() {
        int selectId =15;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
            String sql = "SELECT id,`name`,age,gender FROM student WHERE id >= ?";
            // PreparedStatement 使用预编译的sql
            statement = connection.prepareStatement(sql);
            // 填上？占位符的值
            statement.setInt(1, selectId);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            ArrayList<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 先打开的后关闭
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

    @Test
    public void test3() {
        int selectId =15;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,`name`,age,gender FROM student WHERE id >= ?";
            // PreparedStatement 使用预编译的sql
            statement = connection.prepareStatement(sql);
            // 填上？占位符的值
            statement.setInt(1, selectId);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            ArrayList<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
    }

    @Test
    public void testDelete() {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "DELETE FROM student WHERE id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, 15);
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    @Test
    public void testInsert() {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "INSERT INTO student(name,age,gender) VALUES (?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, "小张");
            statement.setInt(2, 21);
            statement.setString(3, "男");
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    @Test
    public void testUpdate() {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "UPDATE student SET name=?,age=?,gender=? WHERE id=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, "小王");
            statement.setInt(2, 20);
            statement.setString(3, "女");
            statement.setInt(4, 16);
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    @Test
    public void test13() {
        int selectId =15;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT * FROM users WHERE `name`=? AND `password`=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, "zhangsan' OR 1=1 -- y ");
            statement.setString(2, "123");
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String password = resultSet.getString("password");
                System.out.println("name: " + name + ", password: " + password);
            }

        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
    }
}
